package avicit.bdp.dcs.task.rest;

import avicit.bdp.dcs.job.dto.TableDto;
import avicit.bdp.dcs.task.dto.TaskDTO;
import avicit.bdp.dcs.task.service.TaskService;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.httpclient.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间：2020-08-10 15:09
 * @类说明：任务接口
 * @修改记录：
 */
@RestController
@RequestMapping("/api/bdp/dcs/task/TaskRest")
@Api(tags = "任务接口")
public class TaskRest {

    @Autowired
    private TaskService taskService;

    @PostMapping("/saveTasksByJobId")
    @ApiOperation(value = "批量保存任务(第二步中进入下一步)")
    public ResponseMsg saveTasksByJobId(@ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId) throws Exception{
        ResponseMsg responseMsg = new ResponseMsg<>();
        taskService.saveTasksByJob(jobId);
        return responseMsg;
    }

    @GetMapping("/getTaskById")
    @ApiOperation(value = "查看任务")
    public ResponseMsg<TaskDTO> getTaskById(@ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId,
                                            @ApiParam(value = "源表名", name = "srcTableName", required = true) @RequestParam String srcTableName) throws Exception {
        ResponseMsg<TaskDTO> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(taskService.getTaskById(jobId, srcTableName));
        return responseMsg;
    }

    @PostMapping("/updateTask")
    @ApiOperation(value = "更新任务")
    public ResponseMsg updateTask(@RequestBody TaskDTO taskDTO) throws Exception{
        ResponseMsg responseMsg = new ResponseMsg<>();
        taskService.updateTask(taskDTO);
        return responseMsg;
    }

    @GetMapping("/getTableListByJobId")
    @ApiOperation(value = "根据jobId查询增量、全量同步的表(或迁移的表)")
    public ResponseMsg<List<TableDto>> getTableListByJobId(@ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId,
                                                           @ApiParam(value = "同步方式，全量同步：1，增量同步：0。数据迁移不必传该参数", name = "syncMode")
                                                           @RequestParam(required = false) Integer syncMode) throws Exception {
        ResponseMsg<List<TableDto>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(taskService.getTableListByJobId(jobId, syncMode));
        return responseMsg;
    }

    @PostMapping("/checkGrammar")
    @ApiOperation(value = "验证语法")
    public ResponseMsg<Boolean> checkGrammar(@RequestBody TaskDTO taskDTO)  throws Exception{
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(taskService.checkGrammar(taskDTO.getId(), taskDTO.getFilterCondition()));
        return responseMsg;
    }

    @GetMapping("/createTable/{taskId}")
    @ApiOperation(value = "创建表")
    public ResponseMsg<Boolean> createTable(@ApiParam(value = "任务id", name = "taskId", required = true) @PathVariable String taskId) throws Exception{
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        boolean flag = taskService.createTable(taskId);
        responseMsg.setResponseBody(flag);
        if(!flag){
            responseMsg.setRetCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
        }
        return responseMsg;
    }

    @GetMapping("/updateTaskStatus")
    @ApiOperation(value = "更新任务状态")
    public ResponseMsg<Boolean> updateTaskStatus(@ApiParam(value = "taskInstanceId", name = "taskInstanceId", required = true) @RequestParam String taskInstanceId,
                                                 @ApiParam(value = "状态,执行中：1，执行失败：6，暂停中：3，完成：7", name = "status", required = true) @RequestParam Integer status) throws Exception{
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(taskService.updateTaskStatus(taskInstanceId, status));
        return responseMsg;
    }

    @GetMapping("/getSyncFile/{taskId}")
    @ApiOperation(value = "获取待同步的文件")
    public ResponseMsg<String> getSyncFile(@ApiParam(value = "任务id", name = "taskId", required = true) @PathVariable String taskId) throws Exception{
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(taskService.getSyncFile(taskId));
        return responseMsg;
    }

    @GetMapping("/getTaskListByJobId")
    @ApiOperation(value = "查询任务")
    public ResponseMsg<List<TaskDTO>> getTaskListByJobId(@ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId) throws Exception {
        ResponseMsg<List<TaskDTO>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(taskService.getTaskListByJobId(jobId));
        return responseMsg;
    }

    @GetMapping("/preProcess/{taskId}")
    @ApiOperation(value = "前置处理")
    public ResponseMsg<Boolean> preProcess(@ApiParam(value = "任务id", name = "taskId", required = true) @PathVariable String taskId) throws Exception{
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        boolean flag = taskService.preProcess(taskId);
        responseMsg.setResponseBody(flag);
        if(!flag){
            responseMsg.setRetCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
        }
        return responseMsg;
    }

    @ApiOperation(value = "数据预览")
    @GetMapping("/preview")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "jobId", name = "jobId", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "源表名", name = "srcTableName", dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "当前页", name = "currentPage", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
    })
    public ResponseMsg<Map<String, Object>> preview(@RequestParam String jobId,
                                                    @RequestParam String srcTableName,
                                                    @RequestParam(defaultValue = "1") Integer currentPage,
                                                    @RequestParam(defaultValue = "50") Integer pageSize) throws Exception {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(taskService.preview(jobId, srcTableName, currentPage, pageSize));
        return responseMsg;
    }

    @DeleteMapping("/deleteTask")
    @ApiOperation(value = "删除任务")
    public ResponseMsg<Boolean> deleteTask(@RequestBody TaskDTO taskDTO) throws Exception {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        taskService.deleteTask(taskDTO.getJobId(), taskDTO.getSrcTableName());
        responseMsg.setResponseBody(true);
        return responseMsg;
    }

    @GetMapping("/clearSyncTimePointByJobId")
    @ApiOperation(value = "删除增量时间")
    public ResponseMsg clearSyncTimePointByJobId(@ApiParam(value = "jobId", name = "jobId", required = true) @RequestParam String jobId) throws Exception {
        ResponseMsg responseMsg = new ResponseMsg<>();
        taskService.clearSyncTimePointByJobId(jobId);
        return responseMsg;
    }

}
